<?php

if(stristr($_SERVER['PHP_SELF'],".inc.php")) { 
	header("Location:index.php"); 
	exit; 
}
	//show only 15 items per page
	$split 	= "10"; 
	$_ni	= count_posts(); 
	$_p 	= $_REQUEST['p']; 
	if(!$_p || $_p < 1) { $_p = "1"; } 
	$_floor = ($_p * $split) - $split; 
	$_ceil 	= ($_p * $split); 
	$_pages = ceil($_ni/$split); 
	if ($_p > $_pages) { $_p = $_pages; }
	$offset = ($_p*$split) - $split; 
	$posts 	= get_all_posts($offset,$split); 
	$pgl = 1; 
	
	echo "<div style=\"margin-left:auto;margin-right:auto;font-weight:bold;text-decoration:underline;\">"
	. $Lang['postsAreShownBelow']."</div><div style=\"text-align:center;\" id=\"postnav\">\n";
	if($_pages > 1 && $_pages < 10) {
		echo ($_floor+1) . " - " . ($_floor + $split) . "<br />\n ";  
		while($pgl <= $_pages) { 
			$pglinks .= "<a href=\"?op=posts&p=$pgl\">$pgl</a> "; $pgl++; 
		}
	} elseif($_pages >= 10) { 
		//echo "<div id=\"postlegend\">".($_floor+1) . " - " . ($_floor + $split) . "</div>"; 
		if(($_p - 10) > 0) { $pglinks .= "<a href=\"?op=posts&p=1\">1</a> ... "; }
		for($i=-8;$i<=8;$i++) { 
			if($i==0) { $pglinks .= "<span id=\"thispost\">".$_p."</span> "; continue; } 
			if((($_p + $i) > 0) && (($_p+$i) <=$_pages)) { 
				$pglinks .= "<a href=\"?op=posts&p=".($_p+$i)."\">".($_p+$i)."</a> "; 
			}	
		}
		if(($_p +9) <=$_pages) { $pglinks .= " ... <a href=\"?op=posts&p=".$_pages."\">".$_pages."</a> "; }
	}
	if($_pages > 1) { 
		echo trim($pglinks)."</div><br />\n"; 
	}
	
	foreach($posts as $k => $v) {
		$the['id']		= stripslashes($v['id']);
		$the['pubdate']	= $v['pubdate'];
		$the['title'] 	= stripslashes($v['title']);
		$the['teaser'] 	= nl2br(trim(stripslashes($v['teaser'])));
		$the['teaser'] 	= preg_replace( "/\[\[(\S+)\|(.+)\]\]/U", "<a href=\"\\1\">\\2</a>", $the['teaser']);
		$the['teaser']	= str_replace("&pipe;","|",$the['teaser']); 		
		$the['post'] 	= nl2br(trim(stripslashes($v['post'])));
		$the['uid'] 	= $v['uid'];
		$the['time'] 	= date("m/d/Y g:i A",$v['id']);
		$the['public'] 	= trim($v['public']);
		$the['topic']	= $v['topic'];
		$topic 			= get_topic($the['topic']);
		$wordcount		= str_word_count($the['post']);
		$the['edit'] 	= $Lang['editThisPost'];
		$the['delete'] 	= $Lang['deleteThisPost'];
		if($the['public']==1) {
			$the['toggleme'] = $Lang['hideThisPost'];
			$the['toggle'] = "h";
		} else { 
			$the['toggleme'] = $Lang['unhideThisPost'];
			$the['toggle'] = "u";
		}
		if($v['tags']) { 
			unset($the['tags']); 
			if(is_array($v['tags'])) { 	
				foreach($v['tags'] as $Key=>$Val) { 
					$tag = cat_to_tag($Val); 
					$the['tags'] .= trim($Val).", ";
				}
				$the['tags'] = "<span class=\"tag1\">Tags</span>: ".substr(trim($the['tags']),0,-1); 
			} else { 
				$the['tags'] = ''; 		
			}
		} else { 
			$the['tags'] = ''; 
		} 


		#################### TEMPLATE #########################################
		$tmpl = $tmpldir."admin.tmpl";
		if(phpversion() < 4.3) { 
			$html = implode ("", (file($tmpl)));
		} else { 
			$html = file_get_contents($tmpl);  
		}
		//this next bit generates the comment from the HTML template 
		foreach ($the as $key => $value) { 
			$mykey = '{' . $key . '}';
			$html = str_replace($mykey, $value, $html);
		}    
		echo $html;
		#################### END TEMPLATE #####################################
	}
?>